package com.example.restaurant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.restaurant.common.Log;
import com.example.restaurant.common.RestaurantException;
import com.example.restaurant.common.allenum.OrderStatus;
import com.example.restaurant.common.allenum.StoreStatus;
import com.example.restaurant.dao.OrderMapper;
import com.example.restaurant.dao.UserStoreMapper;
import com.example.restaurant.model.Orders;
import com.example.restaurant.model.Store;
import com.example.restaurant.dao.StoreMapper;
import com.example.restaurant.model.UserStore;
import com.example.restaurant.service.StoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.restaurant.service.param.StoreParam;
import com.example.restaurant.service.param.UpdateStoreParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Lang;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 门店表 服务实现类
 * </p>
 *
 * @author x
 * @since 2022-09-29
 */
@Service
@Slf4j
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {
    @Resource
    private StoreMapper storeMapper;
    @Resource
    private UserStoreMapper userStoreMapper;
    @Resource
    private OrderMapper orderMapper;

    @Override
    //新增门店
    @Transactional
    @Log
    public boolean addStore(StoreParam storeParam) {
        log.info("{}",storeParam.toString());
        storeParam.setStatus(StoreStatus.STOP.toString());
        Store store = BeanUtil.copyProperties(storeParam, Store.class);
        int result = storeMapper.insert(store);
//        Integer id = store.getId();

        if(result<1){
            throw new RestaurantException("新增门店失败");
        }
        UserStore userStore = new UserStore();
        userStore.setStoreId(store.getId());
        userStore.setUserId(storeParam.getUserId());
        int insert = userStoreMapper.insert(userStore);
        if(insert<1){
            throw new RestaurantException("新增门店失败");
        }

        return true;
    }

    @Override
    //查询旗下所有门店
    @Log
    public List<Store> getStore(int userId) {
        log.info("{}",userId);
//        if(userId==null||"".equals(userId.trim())){
//            throw new RestaurantException("参数不能为空");
//        }
        List<Store> store = storeMapper.getStore(userId);
        if(store.size()==0){
            throw new RestaurantException("旗下没有门店，请先创建门店");
        }
        return store;
    }

    @Override
    @Log
    @Transactional
    //修改门店信息
    public boolean modifyStore(UpdateStoreParam updateStoreParam) {
        log.info("{}",updateStoreParam);
        Store store = BeanUtil.copyProperties(updateStoreParam, Store.class);
        QueryWrapper<Store> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",updateStoreParam.getId());
        int update = storeMapper.update(store, queryWrapper);
        if(update<1){
            throw new RestaurantException("门店修改失败");
        }
        return true;
    }

    @Override
    @Log
    @Transactional
    //删除门店
    public boolean delStore(int storeId) {
        log.info("{}",storeId);
        if(storeId<1){
            throw new RestaurantException("参数错误");
        }
        List<Orders> orders = orderMapper.selStatusBySId(storeId);
        if(orders.size()!=0){
            throw new RestaurantException("门店有未处理的订单，请先处理");
        }
        int i = storeMapper.deleteById(storeId);
        if(i<1){
            throw new RestaurantException("门店删除失败");
        }
        return true;
    }

    @Override
    public List<Store> getAllStore() {

        List<Store> stores = storeMapper.selectList(null);
        return stores;
    }

}
